﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using MaxLeaf.DB;
using System.Data;
using MySql.Data.MySqlClient;
using System.Transactions;

namespace MaxLeaf.DAL.K3
{
    public class arCustomerDAL : MaxLeaf.DAL.BaseDALBuilder.IDALAction
    {
        
        private struct Detail
        {
            public string Name;
            public string FItemID;
            public string FullName;
            public string FNumber;
            public string Level;
        }

        private struct CustomerInfo
        {
            public string FtypeID;
            public Detail Region;
            public Detail City;            
            public Detail MDC;           
            public Detail Customer;         
        }

        private string GetCustFinterID(DBAccess dbWebCon, DBAccess dbK3Con, string strK3MasterFinterID)
        {
            //说明： 由于在网站中的t_submessage是全局分发的，所以在网站的FinterID是K3主帐套的
            //       的t_submessage的
            string strServerName="K3Master";
           ServerInfo serverK3 = ServerInfo.GetServer(strServerName);
           using (DBAccess dbK3Master = new DBAccess(serverK3))
           {
               return new t_SubMessageDAL().GetDestMapKeyValue(dbK3Master, dbK3Con, strK3MasterFinterID);
           }

        }

        public void Delete(DBAccess dbOrgCon, DBAccess dbCon, string strKeyValue)
        {
            //删除k3数据
            string strUser_id = strKeyValue;            
            string strSQL = @"select USERT.User_ID,USERT.User_Name,MDC.ID as MDC_ID,MDC_Name,MDC.region_id,MDC.region_Name,region_dbname
            from mary_users USERT
            inner join mary_mdc MDC on MDC.ID= USERT.MDC_ID
            where USERT.User_ID ='{0}'";  
            strSQL = string.Format(strSQL, strUser_id);
            DataTable dtTable = dbOrgCon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtTable.Rows)
            {
                string strDB = CvtFunc.ToString(drRow["region_dbname"]);
                if (dbCon.Connection.Database != strDB)
                {
                    LogManager.Debug(string.Format("不在同一数据库！"));
                    return;
                }
            }
            strSQL = @"select FItemID from mary_users where User_ID ='{0}'";  
            strSQL = string.Format(strSQL, strUser_id);
            DataRow drDel = dbOrgCon.GetDataFirstRow(strSQL);
            if (drDel != null)
            {
                if (CvtFunc.ToString(drDel["FItemID"]) != "")
                {
                    strSQL = "Delete from t_Item where FItemID='" + CvtFunc.ToString(drDel["FItemID"]) + "'";
                    dbCon.ExecuteSql(strSQL);
                    strSQL = "Delete from t_organization where FItemID='" + CvtFunc.ToString(drDel["FItemID"]) + "'";
                    dbCon.ExecuteSql(strSQL);
                }
            }
        }
        //读网站的内容，向K3中添加, 
        //此处的server是网站的联接
        //此处的drvLog是从网站的log表中读取的行内容
        //客户编码最后是：   9.分区(2位).MDC(3位).顺序号(6位)
        public void Add(DBAccess dbOrgCon, DBAccess dbCon, string strKeyValue)
        {
            string strUser_id = strKeyValue;
            string strSQL = @"select USERT.User_ID,USERT.User_Name,MDC.ID as MDC_ID,MDC_Name,MDC.region_id,MDC.region_Name,region_dbname,USERT.FItemID,USERT.FTypeID,
                              company_name,shouji,dizhi,youbian,chuanzhen,card_sn,email,office_phone,zhuangtai,content_user,is_use,phone_one
                              from mary_users USERT
                              inner join mary_mdc MDC on MDC.ID= USERT.MDC_ID
                              where USERT.User_ID ='{0}' and ( USERT.FItemID is null or  USERT.FItemID =0 )";  
            strSQL = string.Format(strSQL, strUser_id);
            DataTable dtTable = dbOrgCon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtTable.Rows)
            {
                string strDB = CvtFunc.ToString(drRow["region_dbname"]);
                if (dbCon.Connection.Database != strDB)
                {
                    LogManager.Debug(string.Format("不在同一数据库！"));
                    return;
                }
                
                if (AddCustomer(dbOrgCon, dbCon, drRow))
                {
                    //strSQL = "Delete from mLog where RecID='{0}'";
                    //strSQL = string.Format(strSQL, CvtFunc.ToString(strKeyValue));
                    //dbOrgCon.ExecuteSql(strSQL);
                }
                else
                {
                    throw new MaxLeafException(string.Format("添加客户不成功！"));
                }
            }
        }

        public void Update(DBAccess dbOrgCon, DBAccess dbCon, string strKeyValue)
        {
            string strUser_id = strKeyValue;
            string strSQL = @"select USERT.User_ID,USERT.User_Name,MDC.ID as MDC_ID,MDC_Name,MDC.region_id,MDC.region_Name,region_dbname,USERT.FItemID,USERT.FTypeID,
                              company_name,shouji,dizhi,youbian,chuanzhen,card_sn,email,office_phone,zhuangtai,content_user ,is_use,phone_one
            from mary_users USERT
            inner join mary_mdc MDC on MDC.ID= USERT.MDC_ID
            where USERT.User_ID ='{0}' ";            
            strSQL = string.Format(strSQL, strUser_id);
            DataTable dtTable = dbOrgCon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtTable.Rows)
            {
                string strDB = CvtFunc.ToString(drRow["region_dbname"]);
                string strServerName = "K3Master";
               
                if (dbCon.Connection.Database != strDB)
                {
                    LogManager.Debug(string.Format("不在同一数据库！"));
                    return;
                }
                //ServerInfo serverK3 = ServerInfo.GetServer(strServerName);
                //serverK3.Database = strDB;
                //using (DBAccess mycon = new DBAccess(serverK3))
                //{
                    if (UpateCustomer( dbOrgCon , dbCon , drRow))
                    {
                        //strSQL = "Delete from mLog where ID='{0}'";
                        //strSQL = string.Format(strSQL, CvtFunc.ToString(drRow["RecID"]));
                        //dbOrgCon.ExecuteSql(strSQL);
                        return ;
                    }
                    else
                    {
                        throw new MaxLeafException(string.Format("更新客户不成功！"));
                    }
                //}
            }
            return ;
        }
        //INSERT 到 Fitem表，获得新的Fitem，并要回传网站
        private bool AddCity(DBAccess WebCon , DBAccess  k3Con, DataRow drWebRow, ref CustomerInfo curCust)
        {
            //City的Flevel=2
            string strCity = curCust.City.Name ;
            string strFullName = curCust.City.FullName;            
            {
                curCust.City.FNumber = curCust.Region.FNumber +"."+GetMaxFnumber ( k3Con, curCust.City.Level , ref curCust);                 
                string strSQL = @"insert into t_Item (FitemClassID,FNumber,FparentID,Flevel,Fdetail,Fname,FUnUsed,FBrNo,FFullNumber,
                                  FDiff,FDeleted,FshortNumber,FFullName) 
                                  VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}')";
                strSQL = string.Format ( strSQL ,'1'
                                                , curCust.City.FNumber
                                                , curCust.Region.FItemID
                                                , curCust.City.Level 
                                                , 0
                                                , curCust.City.Name 
                                                , 0
                                                , 0
                                                , curCust.City.FNumber
                                                , 0
                                                , 0
                                                , curCust.City.FNumber 
                                                , curCust.City.FullName );
                k3Con.ExecuteSql(strSQL);
                strSQL = "select FItemID from t_Item where FitemClassID='1' and FFullNumber='" + curCust.City.FNumber + "' and FFullName='" + curCust.City.FullName + "'";
                curCust.City.FItemID = k3Con.GetValue(strSQL);

                //更新WEB，回写FItemID到网站的marry_region表
                //strSQL ="update marry_region set FItemID='"+  +"' where region_id='"+ CvtFunc.ToString(drWebRow["region_id"]) +"'";
            }
            return true;
        }

        private bool AddMDC(DBAccess WebCon, DBAccess k3Con, DataRow drWebRow, ref CustomerInfo curCust)
        {
            curCust.MDC.FNumber = curCust.City.FNumber +"."+ GetMaxFnumber(k3Con, curCust.MDC.Level, ref curCust);
            string strSQL = @"insert into t_Item (FitemClassID,FNumber,FparentID,Flevel,Fdetail,Fname,FUnUsed,FBrNo,FFullNumber,
                                  FDiff,FDeleted,FshortNumber,FFullName) 
                                  VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}')";
            strSQL = string.Format(strSQL, '1'
                                            , curCust.MDC.FNumber
                                            , curCust.City.FItemID
                                            , curCust.MDC.Level
                                            , 0
                                            , curCust.MDC.Name
                                            , 0
                                            , 0
                                            , curCust.MDC.FNumber
                                            , 0
                                            , 0
                                            , curCust.MDC.FNumber
                                            , curCust.MDC.FullName);
            strSQL += ";select @@identity";
            k3Con.ExecuteSql(strSQL);
            strSQL = "select FItemID from t_Item where FitemClassID='1' and FFullNumber='" + curCust.MDC.FNumber + "' and FFullName='" + curCust.MDC.FullName + "'";
            string strFnumber = k3Con.GetValue(strSQL);
            curCust.MDC.FItemID = strFnumber;

            //更新WEB，回写FItemID到网站的mary_mdc表
            strSQL ="update mary_mdc set FItemID='"+ curCust.MDC.FItemID +"' where id='"+ CvtFunc.ToString(drWebRow["MDC_ID"]) +"'";
            WebCon.ExecuteSql(strSQL);   
            return true;
        }

        private bool AddCustomer(DBAccess dbWebCon, DBAccess k3Con, DataRow drWebRow)
        {
            CustomerInfo curCust = new CustomerInfo();
            //获得9开头的区域作用终端大区            
            string strSQL = "select * from t_Item where FitemClassID='1' and Flevel=1 and FDetail=0 and FNumber='9'";
            DataRow drRegion = k3Con.GetDataFirstRow(strSQL);
            if (drRegion == null) return false;//网站数据有错，返回；错误原因：大区 or 分区 在网站中没有对应关系。

            curCust.Region.Name = CvtFunc.ToString(drRegion["FName"]);
            curCust.Region.FullName = CvtFunc.ToString(drRegion["FFullName"]);
            curCust.Region.FNumber = "9"; 
            curCust.Region.Level ="1";
            curCust.Region.FItemID = CvtFunc.ToString(drRegion["FItemID"]);
   
            curCust.City.Name = CvtFunc.ToString(drWebRow["Region_name"]);
            curCust.City.FullName = curCust.Region.FullName + "_" + curCust.City.Name;
            curCust.City.Level ="2";
            
            curCust.MDC.Name = CvtFunc.ToString(drWebRow["MDC_Name"]);
            curCust.MDC.FullName = curCust.City.FullName + "_" + curCust.MDC.Name;
            curCust.MDC.Level ="3";

            curCust.Customer.Name = CvtFunc.ToString(drWebRow["company_name"]);
            curCust.Customer.FullName = curCust.MDC.FullName + "_" + curCust.Customer.Name;
            curCust.Customer.Level ="4";
            if (!checkRegion(dbWebCon, k3Con, drWebRow, ref curCust ))
            {                
                LogManager.Debug(string.Format("添加大区不成功！"));
                return false;
            }

            if (!checkCity(dbWebCon, k3Con, drWebRow, ref curCust))
            {
                LogManager.Debug(string.Format("添加分区不成功！"));
                return false;
            }

            if (!CheckMDC(dbWebCon, k3Con, drWebRow, ref curCust))
            {
                LogManager.Debug(string.Format("添加MDC不成功！"));
                return false;
            }
            string strMaxFnumber = GetMaxFnumber(k3Con,"4",ref curCust);
            curCust.Customer.FNumber = curCust.MDC.FNumber + "." + strMaxFnumber;
            
            string strFInterID = GetCustFinterID(dbWebCon, k3Con, CvtFunc.ToString (drWebRow["FTypeID"]));
            curCust.FtypeID = strFInterID;
 
            AddCustomerDetail(k3Con, drWebRow, ref curCust);
            
            #region 更新WEB，回写FItemID到网站的mary_mdc表
            strSQL = "update mary_users set FItemID='" + curCust.Customer.FItemID + "' where user_id='" + CvtFunc.ToString(drWebRow["User_ID"]) + "'";
            dbWebCon.ExecuteSql(strSQL);
            #endregion
            return true;
        }

        private static void AddCustomerDetail(DBAccess k3Con, DataRow drWebRow, ref CustomerInfo curCust)
        {
            string strSQL = "";
            #region 添加新的customer到K3的表中
            strSQL = @"insert into t_Item (FitemClassID,FNumber,FparentID,Flevel,Fdetail,Fname,FUnUsed,FBrNo,FFullNumber,
                                  FDiff,FDeleted,FshortNumber,FFullName) 
                                  VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}')";
            strSQL = string.Format(strSQL, '1'
                                            , curCust.Customer.FNumber
                                            , curCust.MDC.FItemID
                                            , curCust.Customer.Level
                                            , 1
                                            , curCust.Customer.Name
                                            , 0
                                            , 0
                                            , curCust.Customer.FNumber
                                            , 0
                                            , 0
                                            , curCust.Customer.FNumber
                                            , curCust.Customer.FullName);
            k3Con.ExecuteSql(strSQL);

            strSQL = "select FItemID from t_Item where FitemClassID='1' and FFullNumber='" + curCust.Customer.FNumber + "' and FFullName='" + curCust.Customer.FullName + "'";
            curCust.Customer.FItemID = k3Con.GetValue(strSQL);


            strSQL = @" insert into t_organization ( Fname,fmobilephone,faddress,fpostalcode,ffax,f_109,femail,fphone,fstatus,fcontact,FtypeID,FItemID,
                                                    Fnumber,FParentID,FshortNumber,FSaleID,fdeleted)
                        values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}',
                                                    '{12}','{13}','{14}','{15}','{16}'   )";
            strSQL = string.Format(strSQL, curCust.Customer.Name
                                            , CvtFunc.ToString(drWebRow["shouji"])
                                            , CvtFunc.ToString(drWebRow["dizhi"])
                                            , CvtFunc.ToString(drWebRow["youbian"])
                                            , CvtFunc.ToString(drWebRow["chuanzhen"])
                                            , CvtFunc.ToString(drWebRow["card_sn"])
                                            , CvtFunc.ToString(drWebRow["email"])
                                            , CvtFunc.ToString(drWebRow["phone_one"]) + "," + CvtFunc.ToString(drWebRow["shouji"])
                                            , "1072"//CvtFunc.ToString(drWebRow["zhuangtai"])
                                            , CvtFunc.ToString(drWebRow["content_user"])
                                            , CvtFunc.ToString(curCust.FtypeID)
                                            , curCust.Customer.FItemID
                                            , curCust.Customer.FNumber
                                            , curCust.MDC.FItemID 
                                            , curCust.Customer.FNumber.Substring(9,6)
                                            , "100"
                                            ,  ( CvtFunc.ToInt (drWebRow["is_use"])==1?0:1));
            k3Con.ExecuteSql(strSQL); 
            #endregion
        }

        private string GetMaxFnumber(DBAccess k3Con, string strLevel, ref CustomerInfo curCust)
        {
            //客户编码最后是：   9.分区(2位).MDC(3位).顺序号(6位)  如: 9.01.001.000001
            string strReturn = "";
            string strSQL="";
            int  intPoint = 1;
            if ( strLevel =="4")  // 客户顺序号
            {
                strSQL = "select substring(cast(max(cast( replace(fnumber,'.','') as decimal(18,0))) as varchar(50)),7,6) from t_Item where FitemClassID='1' and Flevel='" + strLevel + "' and fnumber like '" + curCust.MDC.FNumber + "%'";
                intPoint = 6;
            }
            else if ( strLevel =="3") // MDC地址
            {
                strSQL = "select substring(cast(max(cast( replace(fnumber,'.','') as decimal(18,0))) as varchar(50)),4,3) from t_Item where FitemClassID='1' and Flevel='" + strLevel + "' and fnumber like '" + curCust.City.FNumber + "%'";
                intPoint = 3;
            }
            else if (strLevel == "2") //分区
            {
                strSQL = "select substring(cast(max(cast( replace(fnumber,'.','') as decimal(18,0))) as varchar(50)),2,2) from t_Item where FitemClassID='1' and Flevel='" + strLevel + "' and fnumber like '" + curCust.Region.FNumber + "%'";
                intPoint = 2;
            }
            DataRow drRow = k3Con.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                strReturn = BaseFunctions.FormatWithZero(CvtFunc.ToString (   CvtFunc.ToDouble(CvtFunc.ToString(drRow[0])) + 1 ),intPoint);
            }            
            return strReturn;
        }

        private bool AddRegion(DBAccess k3Con, string strRegion)
        {
            //大区不能自动添加。
            return true ;
        }

        private bool UpateCustomer(DBAccess dbWebCon, DBAccess k3Con, DataRow drWebRow)
        {
            CustomerInfo curCust = new CustomerInfo();
            string strRegion_id = CvtFunc.ToString(drWebRow["Region_id"]);
            string strSQL = "select * from t_Item where FitemClassID='1' and Flevel=1 and FDetail=0 and FNumber='9'";
            DataRow drRegion = k3Con.GetDataFirstRow(strSQL);
            if (drRegion == null) return false;//网站数据有错，返回；错误原因：大区 or 分区 在网站中没有对应关系。

            curCust.Region.Name = CvtFunc.ToString(drRegion["FName"]);
            curCust.Region.FullName = CvtFunc.ToString(drRegion["FFullName"]);
            curCust.Region.FNumber = "9";
            curCust.Region.Level = "1";
            curCust.Region.FItemID = CvtFunc.ToString(drRegion["FItemID"]);

            //curCust.Region.Name = CvtFunc.ToString(drRegion["ParentName"]);
            //curCust.Region.FullName = CvtFunc.ToString(drRegion["ParentName"]);
            //curCust.Region.FNumber = "9";
            //curCust.Region.Level = "1";

            curCust.City.Name = CvtFunc.ToString(drWebRow["Region_name"]);
            curCust.City.FullName = curCust.Region.FullName + "_" + curCust.City.Name;
            curCust.City.Level = "2";
            curCust.MDC.Name = CvtFunc.ToString(drWebRow["MDC_Name"]);
            curCust.MDC.FullName = curCust.City.FullName + "_" + curCust.MDC.Name;
            curCust.MDC.Level = "3";
            curCust.Customer.Name = CvtFunc.ToString(drWebRow["company_name"]);
            curCust.Customer.FullName = curCust.MDC.FullName + "_" + curCust.Customer.Name;
            curCust.Customer.FItemID = CvtFunc.ToString(drWebRow["FItemID"]);
            
            //不存在就添加
            if (k3Con.GetTableDataFirstRow("t_Organization", new Dictionary<string, object>() { { "FitemId", curCust.Customer.FItemID } }) == null)
            {
                strSQL = "select * from t_item where FName='" + curCust.Customer.Name + "' and FitemClassID='1' and FNumber like '9.%' ";
                DataRow drCheckCust = k3Con.GetDataFirstRow(strSQL);
                if (drCheckCust == null) //检查有没有以前加过的，如果以前这个客户存在，只是MDC换了，则不能用添加客户，应该只是更新原来的客户。
                {
                    return AddCustomer(dbWebCon, k3Con, drWebRow);
                }
                else
                {
                    curCust.Customer.FItemID = CvtFunc.ToString(drCheckCust["FItemID"]);
                }
            }

            curCust.Customer.Level = "4";
            if (!checkRegion(dbWebCon, k3Con, drWebRow, ref curCust))
            {
                throw new MaxLeafException(string.Format("添加大区不成功！"));
                return false;
            }

            if (!checkCity(dbWebCon, k3Con, drWebRow, ref curCust))
            {
                throw new MaxLeafException(string.Format("添加分区不成功！"));
                return false;
            }
            if (!CheckMDC(dbWebCon, k3Con, drWebRow, ref curCust))
            {
                throw new MaxLeafException(string.Format("添加MDC不成功！"));
                return false;
            }
            if (!checkCustomer(dbWebCon, k3Con, drWebRow, ref curCust))  //如果这个customer没有改过区域orMDC，则不用重新分配 Fnumber 和 FFullName,否则要重新分配Fnumber和FFullName
            {                
                string strMaxFnumber = GetMaxFnumber(k3Con, "4", ref curCust);
                curCust.Customer.FNumber = curCust.MDC.FNumber + "." + strMaxFnumber;
            }

            string strFInterID = GetCustFinterID(dbWebCon, k3Con, CvtFunc.ToString(drWebRow["FTypeID"]));
            curCust.FtypeID = strFInterID;

            strSQL = @"update t_Item set FitemClassID='{0}'
                                        ,FNumber='{1}'
                                        ,FparentID='{2}'
                                        ,Flevel='{3}'
                                        ,Fdetail='{4}'
                                        ,Fname='{5}'
                                        ,FUnUsed='{6}'
                                        ,FBrNo='{7}'
                                        ,FFullNumber='{8}'
                                        ,FDiff='{9}'
                                        ,FDeleted='{10}'
                                        ,FshortNumber='{11}'
                                        ,FFullName='{12}' where FItemID='{13}'";
            strSQL = string.Format(strSQL, '1'
                                            , curCust.Customer.FNumber
                                            , curCust.MDC.FItemID
                                            , curCust.Customer.Level
                                            , 1
                                            , curCust.Customer.Name
                                            , 0
                                            , 0
                                            , curCust.Customer.FNumber
                                            , 0
                                            , 0
                                            , curCust.Customer.FNumber
                                            , curCust.Customer.FullName
                                            , curCust.Customer.FItemID);
            k3Con.ExecuteSql(strSQL);

            //strSQL = "select FItemID from t_Item where FitemClassID='1' and FNumber='" + curCust.Customer.FNumber + "' and FFullNumber='" + curCust.Customer.FullName + "'";
            //DataRow drCheck = k3Con.GetDataFirstRow(strSQL);
            //if (drCheck != null)
            //{
            //    curCust.Customer.FItemID = k3Con.GetValue(strSQL);
            //}
            #region 添加新的customer到K3的表中
            strSQL = @" update t_organization set Fname='{0}'
                                                ,fmobilephone='{1}'
                                                ,faddress='{2}'
                                                ,fpostalcode='{3}'
                                                ,ffax='{4}'
                                                ,f_109='{5}'
                                                ,femail='{6}'
                                                ,fphone='{7}'
                                                ,fstatus='{8}'
                                                ,fcontact='{9}'
                                                ,ftypeID='{10}' 
                                                ,Fnumber='{11}'
                                                ,FParentID='{12}'
                                                ,FshortNumber ='{13}'
                                                ,Fdeleted='{14}'
                                                ,FSaleID='100'
                                                where FItemID='{15}'";
            strSQL = string.Format(strSQL, CvtFunc.ToString(drWebRow["company_name"])
                                            , CvtFunc.ToString(drWebRow["shouji"])
                                            , CvtFunc.ToString(drWebRow["dizhi"])
                                            , CvtFunc.ToString(drWebRow["youbian"])
                                            , CvtFunc.ToString(drWebRow["chuanzhen"])
                                            , CvtFunc.ToString(drWebRow["card_sn"])
                                            , CvtFunc.ToString(drWebRow["email"])
                                            , CvtFunc.ToString(drWebRow["phone_one"]) + "," + CvtFunc.ToString(drWebRow["shouji"])
                                            , "1072"//CvtFunc.ToString(drWebRow["zhuangtai"])
                                            , CvtFunc.ToString(drWebRow["content_user"])
                                            , curCust.FtypeID
                                            , curCust.Customer.FNumber
                                            , curCust.MDC.FItemID                                            
                                            , curCust.Customer.FNumber.Substring(9, 6)
                                            , ( CvtFunc.ToInt (drWebRow["is_use"])==1?0:1)
                                            , curCust.Customer.FItemID
                                            );
            k3Con.ExecuteSql(strSQL);
            #endregion

            #region 更新WEB，回写FItemID到网站的mary_mdc表
            strSQL = "update mary_users set FItemID='" + curCust.Customer.FItemID + "' where user_id='" + CvtFunc.ToString(drWebRow["User_ID"]) + "'";
            dbWebCon.ExecuteSql(strSQL);
            #endregion
            return true;
        }


        #region 检查客户
        private bool checkCustomer(DBAccess WebCon, DBAccess k3Con, DataRow drWebRow, ref CustomerInfo curCust)
        {
            string strSQL = "select A.* from t_item A inner join t_Organization B on A.FItemID=B.FItemID where A.FitemClassID=1 ";
                   strSQL +=" and A.FItemID='" + curCust.Customer.FItemID + "' and FFullName like '" + curCust.MDC.FullName + "%'" ; // and Fnumber='"+ curCust.Customer.FNumber +"'";
            DataRow drCust = k3Con.GetDataFirstRow(strSQL);
            if (drCust == null) return false;  // Customer在K3中不存在。
            curCust.Customer.FItemID = CvtFunc.ToString(drCust["FItemID"]);
            curCust.Customer.FNumber = CvtFunc.ToString(drCust["Fnumber"]);
            return true;
        }
        #endregion

        #region 检查大区
        private bool checkRegion(DBAccess WebCon, DBAccess k3Con, DataRow drWebRow, ref CustomerInfo curCust)
        {           
            string strSQL ="select * from t_item where FitemClassID=1 and FFullName='"+ curCust.Region.FullName +"'";
            DataRow drRegion = k3Con.GetDataFirstRow(strSQL);
            if (drRegion == null) return false;  // Region在K3中不存在，不允许自动添加.因为大区是要对应数据库的。
            curCust.Region.FItemID = CvtFunc.ToString(drRegion["FItemID"]);
            curCust.Region.FNumber = CvtFunc.ToString(drRegion["Fnumber"]); 
            return true;
        }
        #endregion

        #region 检查城区
        private bool  checkCity(DBAccess WebCon, DBAccess k3Con, DataRow drWebRow, ref CustomerInfo curCust)
        {
            string strSql = "select * from t_Item where FitemclassID=1 and Flevel='2' and FFullName='{0}'";
            strSql = string.Format(strSql, curCust.City.FullName);
            DataRow drCity = k3Con.GetDataFirstRow( strSql); 
            if (drCity == null)  //不存在分区就要添加
            {
                AddCity(WebCon, k3Con, drWebRow, ref curCust);              
            }
            drCity = k3Con.GetDataFirstRow(strSql); 
            if ( drCity !=null) 
            {
                curCust.City.FItemID = CvtFunc.ToString(drCity["FItemID"]);
                curCust.City.FNumber = CvtFunc.ToString(drCity["FNumber"]);
                curCust.City.FullName = CvtFunc.ToString(drCity["FFullName"]);
                curCust.City.Name = CvtFunc.ToString(drCity["FName"]);  
                return true;
            }
            return false;
        }
        #endregion

        #region 检查MDC
        private bool CheckMDC(DBAccess WebCon, DBAccess k3Con, DataRow drWebRow, ref CustomerInfo curCust)
        {
            string strSql = "select * from t_Item where FitemclassID=1 and Flevel='3' and FFullName='{0}'";
            strSql = string.Format(strSql, curCust.MDC.FullName);
            DataRow drMDC = k3Con.GetDataFirstRow(strSql);
            if (drMDC == null)  //不存在分区就要添加
            {
                AddMDC(WebCon, k3Con, drWebRow, ref curCust);               
            }
            drMDC = k3Con.GetDataFirstRow(strSql);
            if (drMDC !=null) 
            {
                curCust.MDC.FItemID = CvtFunc.ToString(drMDC["FItemID"]);
                curCust.MDC.FNumber = CvtFunc.ToString(drMDC["FNumber"]);
                curCust.MDC.FullName = CvtFunc.ToString(drMDC["FFullName"]);
                curCust.City.Name = CvtFunc.ToString(drMDC["FName"]);
                return true;
            }
            return false;
        }

        #endregion
    }        
}
